<template>
  <div ref="scroll">
    <div>
      <slot></slot>
    </div>
  </div>
</template>
<script>
import BScroll from '@better-scroll/core';
import PullUp from '@better-scroll/pull-up'
import ObserveDom from '@better-scroll/observe-dom'

BScroll.use(ObserveDom)
BScroll.use(PullUp);

export default {
  name: "Scroll",
  data: () => ({
    bs: {
      type: Object,
      default: () => null
    },
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  }),
  mounted() {
    this.bs = new BScroll(this.$refs.scroll, {
      click: true, // 允许被包裹的组件可以监听到 click 事件
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad,
      observeDOM: true
    });

    if (this.pullUpLoad || this.probeType > 1) {
      this.bs.on('scroll', position => {
        this.$emit('scroll', position);
      });
    }
    if (this.pullUpLoad) {
      this.bs.on('pullingUp', () => {
        this.$emit('pull-up');
      });
    }
  },
  methods: {
    scrollTo(x, y, time=300) {
      if (this.bs) {
        this.bs.scrollTo(x, y, time);
      }
    },
    finishPullUp() {
      this.bs.finishPullUp();
    },
    getScrollY() {
      return this.bs ? this.bs.y : undefined;
    },
    refresh() {
      this.bs && this.bs.refresh();
    }
  }
}
</script>
<style scoped>
</style>
